package ai.people.netmon.warning.service.impl;

import ai.people.netmon.framework.domain.warning.entity.WarningAccountLabelRelation;
import ai.people.netmon.framework.domain.warning.entity.WarningLabelTree;
import ai.people.netmon.framework.domain.warning.request.label.WarningLabelTreeVO;
import ai.people.netmon.warning.mapper.WarningLabelTreeMapper;
import ai.people.netmon.warning.service.WarningLabelTreeService;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
* @author ligua
* @description 针对表【warning_label_tree(标签库-标签)】的数据库操作Service实现
* @createDate 2022-08-08 20:45:34
*/
@Service
@RequiredArgsConstructor
public class WarningLabelTreeServiceImpl extends ServiceImpl<WarningLabelTreeMapper, WarningLabelTree>
    implements WarningLabelTreeService{


    private final WarningLabelTreeMapper warningLabelTreeMapper;


    @Override
    public List<WarningLabelTreeVO> listLabelTree(Long parentId) {
        List<WarningLabelTreeVO> result = new ArrayList<>();

        List<WarningLabelTree> list = list();
        Map<Long, List<WarningLabelTree>> mapping = list.stream().collect(Collectors.groupingBy(WarningLabelTree::getParentId));

        //递归构造树
        List<WarningLabelTree> labelLibs = mapping.get(parentId);
        if(!CollectionUtils.isEmpty(labelLibs)){
            for (WarningLabelTree root : labelLibs) {
                result.add(buildTree(root, mapping));
            }
        }
        return result;
    }

    /**
     * 所有标签
     *
     * @return {@link List}<{@link WarningLabelTree}>
     */
    @Override
    public List<WarningLabelTree> allLabel() {
        return warningLabelTreeMapper.selectList(Wrappers.lambdaQuery());
    }

    /**
     * 所有标签值映射
     *
     * @return {@link Map}<{@link String}, {@link WarningLabelTree}>
     */
    @Override
    public Map<String, WarningLabelTree> allLabelValueMapping() {
        return this.allLabel().stream().collect(Collectors.toMap(WarningLabelTree::getLabelValue, Function.identity(), (warningLabelTree, warningLabelTree2) -> warningLabelTree));
    }

    /**
     * 通过账户查询标签
     *
     * @param accountIds 帐户id
     * @return {@link List}<{@link WarningAccountLabelRelation}>
     */
    @Override
    public List<WarningAccountLabelRelation> queryLabelByAccount(List<Long> accountIds) {
        return warningLabelTreeMapper.queryLabelByAccount(accountIds);
    }

    private WarningLabelTreeVO buildTree(WarningLabelTree root, Map<Long, List<WarningLabelTree>> mapping) {
        WarningLabelTreeVO node = new WarningLabelTreeVO(root);
        List<WarningLabelTreeVO> childrenVo = new ArrayList<>();
        List<WarningLabelTree> children = mapping.get(root.getId());
        if (CollectionUtils.isNotEmpty(children)) {
            for (WarningLabelTree child : children) {
                childrenVo.add(buildTree(child, mapping));
            }
            //递归计算每个父节点的子节点数量
            int sum = childrenVo.stream().mapToInt(WarningLabelTreeVO::getChildrenCnt).sum() + childrenVo.size();
            node.setChildrenCnt(sum);
        } else {
            node.setChildrenCnt(0);
        }
        node.setChildren(CollectionUtils.isEmpty(childrenVo) ? null : childrenVo);
        return node;
    }
}




